package com.leyou.cart.service;

import com.leyou.cart.entity.Cart;
import com.leyou.common.auth.entity.UserInfo;
import com.leyou.common.enums.ExceptionEnum;
import com.leyou.common.exceptions.LyException;
import com.leyou.common.thrreadlocals.UserHolder;
import com.leyou.common.utils.JsonUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.BoundHashOperations;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @Auther: xiang
 * @Date: 2019/9/1 20:47
 * @Description:
 */
@Service
@Transactional
public class CartService {
    private static final String KEY_PREFIX = "ly:cart:uid:";
    @Autowired
    private StringRedisTemplate redisTemplate;
/*    @Autowired
    private CartMapper cartMapper;*/

    public void addCart(Cart cart) {
        //获得当前用户
        String key = KEY_PREFIX + UserHolder.getUser().getId();
        // 获取hash操作的对象，并绑定用户id
        BoundHashOperations<String, String, String> hashOps = redisTemplate.boundHashOps(key);
        addCartList(cart, hashOps);

    }

    private void addCartList(Cart cart, BoundHashOperations<String, String, String> hashOps) {
        //获得商品Id，作为hashKey
        String hashKey = cart.getSkuId().toString();
        //获得数量
        Integer num = cart.getNum();
        //判断要添加的用户是否存在
        Boolean aBoolean = hashOps.hasKey(hashKey);
        if (aBoolean && aBoolean != null) {
            //存在修改数量
            cart.setNum(num + cart.getNum());
        }
        hashOps.put(hashKey, JsonUtils.toString(cart));
    }

    /**
     * 查询购物车
     */
    public List<Cart> queryCartList() {
        //获取登录用户
        UserInfo user = UserHolder.getUser();
        //获得登陆用户的hashid
        String Key = KEY_PREFIX + user.getId();
        Boolean aBoolean = redisTemplate.hasKey(Key);
        if (!aBoolean || aBoolean == null) {
            throw new LyException(ExceptionEnum.CARTS_NOT_FOUND);

        }
        BoundHashOperations<String, String, String> hashOps = this.redisTemplate.boundHashOps(Key);
        List<String> carts = hashOps.values();
        boolean b = CollectionUtils.isEmpty(carts);
        if (b){
            // 不存在，直接返回
            throw new LyException(ExceptionEnum.CARTS_NOT_FOUND);
        }
      return    carts.stream().map(json->JsonUtils.toBean(json,Cart.class)).collect(Collectors.toList());
    }

    /**
     * 批量新增购物车,就是把集合每一个商品添加到购物车
     * 可以调用之前的单个新增
     * @param cartList
     */
    public void addCartList(List<Cart> cartList) {
        // 获取当前用户
        String key = KEY_PREFIX + UserHolder.getUser().getId();

        // 获取hash操作的对象，并绑定用户id
        BoundHashOperations<String, String, String> hashOps = redisTemplate.boundHashOps(key);
        for (Cart cart : cartList) {

            addCartList(cart,hashOps);


        }


    }
}